package com.xiyue.leaspring.action;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Locale;

@Controller//定义控制器
@RequestMapping("/pages")//定义访问父路径，与方法中过的路径组合为完整的路径
@Slf4j//注入日志实例
public class EchoAction {//自定义程序类

    @Autowired
    private MessageSource messageSource;

    @InitBinder
    public void initBinder(WebDataBinder binder){//设置一个Web数据的绑定转换
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");//定义转换处理
        //在web数据绑定中注册自定义规则绑定器,主要用于处理java.util.Date类型，允许为null
        binder.registerCustomEditor(java.util.Date.class,new CustomDateEditor(sdf,true));
    }

    @RequestMapping("/echo/{msg}")//访问的路径为echo.action
    public ModelAndView echo(@PathVariable(name = "msg") String msg){//根据参数名称自动进行匹配处理
        log.info("*****EchoAction接收到请求参数，msg"+msg);//日志输出
        //日志输出ModeAndView主要功能时设置跳转路径以及保存request属性
        ModelAndView mav = new ModelAndView("/message/message_show");
        mav.addObject("echoMessage","【Echo】msg="+msg);
        return mav;
    }

    @RequestMapping("/echo1")
    public ModelAndView echo1(
            @RequestParam(//表示对请求参数的配置
                name = "msg",//表示映射的请求参数的名称
                required = false,//表示该参数是否比逊传递
                defaultValue = "天选"//表示该参数为null时的默认值
            )
            String msg){//接收请求参数
        log.info("*****EchoAction接收到请求参数，msg"+msg);//日志输出
        //日志输出ModeAndView主要功能时设置跳转路径以及保存request属性
        ModelAndView mav = new ModelAndView("/message/message_show");
        mav.addObject("echoMessage","【Echo】msg="+msg);
        return mav;
    }

    @RequestMapping("/inputView")//访问的路径为echo.action
    public String inputView(){//根据参数名称自动进行匹配处理
        return "/message/message_input1";
    }
    @RequestMapping("/input")//访问的路径为echo.action
    public ModelAndView input(@RequestParam String msg
        , Integer level, String tags[], Date pubdate){//根据参数名称自动进行匹配处理
        ModelAndView modelAndView = new ModelAndView("/message/show");
        modelAndView.addObject("echoMessage","【Echo】msg="+msg);
        modelAndView.addObject("echolevel","【Echo】level="+level);
        modelAndView.addObject("echoTags","【Echo】tags="+ Arrays.toString(tags));
        modelAndView.addObject("echoPubDate","【Echo】pubdate="+pubdate);
        return modelAndView;
    }

    @RequestMapping("/inputView1")//访问的路径为echo.action
    public String inputView1(){//根据参数名称自动进行匹配处理
        return "/pages/message/message_input1";
    }
    @RequestMapping("/input1")//访问的路径为echo.action
    public ModelAndView input1(@RequestParam String msg
            , Integer level, String tags[], Date pubdate){//根据参数名称自动进行匹配处理
        ModelAndView modelAndView = new ModelAndView("/pages/message/show");
        modelAndView.addObject("echoMessage","【Echo】msg="+msg);
        modelAndView.addObject("echolevel","【Echo】level="+level);
        modelAndView.addObject("echoTags","【Echo】tags="+ Arrays.toString(tags));
        modelAndView.addObject("echoPubDate","【Echo】pubdate="+pubdate);
        return modelAndView;
    }

    @GetMapping("/message")
    public ModelAndView messageView(){
        log.info("【echo.input.page】"+this.messageSource
                .getMessage("echo.input.page",null, Locale.getDefault()));
        log.info("【welcome.info】"+this.messageSource
                .getMessage("welcome.info",new Object[]{"喜悦"}, Locale.getDefault()));
        log.info("【noting】"+this.messageSource
                .getMessage("nothing",null, Locale.getDefault()));
        return null;
    }

}
